package com.csdn.runner;

import com.csdn.config.BaseConfig;
import com.csdn.listener.ConfigClassResolver;
import com.csdn.reposity.ConfigReposity;
import com.csdn.reposity.ConfigReposityHolder;
import com.csdn.reposity.FileConfigReposity;
import com.csdn.reposity.LoadConfigTask;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Order(value = Integer.MAX_VALUE + 10)
@Component
public class ConfigInitialzer implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取所有配置信息
        Map<String, Class<? extends BaseConfig>> classMap = ConfigClassResolver.getClassMap();
        //TODO spi加载有问题
       // ConfigReposity configReposity = ConfigReposityHolder.get("FILE");
        ConfigReposity configReposity = new FileConfigReposity();

        //异步加载数据
        Set<Class<? extends BaseConfig>> collects = classMap.values().stream().collect(Collectors.toSet());
        //添加监听
        configReposity.initial(collects);
        for (Class<? extends BaseConfig> c : collects) {
            CompletableFuture.runAsync(new LoadConfigTask(configReposity, c));
        }

    }
}
